package io.sage.modules.basics.controller;

import java.util.*;

import io.sage.common.utils.StringUtils;
import io.sage.modules.sys.entity.SysDeptEntity;
import io.sage.modules.sys.service.SysDeptService;
import io.sage.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.sage.common.utils.R;
import io.sage.modules.basics.entity.BWarehouse;
import io.sage.modules.basics.service.BWarehouseService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;

/**
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2018-07-09 13:48:15
 */
@RestController
@RequestMapping("basics/bwarehouse")
public class BWarehouseController {
	
	@Autowired
	private BWarehouseService warehouseService;

	@Autowired
	private SysDeptService sysDeptService;

	@Autowired
	private SysUserService sysUserService;

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("basics:bwarehouse:list")
	public R list(@RequestParam Map<String, Object> params) {
		Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		if(params.get("antennaName")!=null){

			if(!"".equals(params.get("antennaName").toString())){
				paramMap.put("antennaName", params.get("antennaName").toString());
			}
		}

		if(params.get("unitCode")!=null){
			if(!"".equals(params.get("unitCode").toString())){
				paramMap.put("unitCode", params.get("unitCode").toString());
			}
		}

		if(params.get("departmentId")!=null){
			if(!"".equals(params.get("departmentId").toString())){
				paramMap.put("departmentId", params.get("departmentId").toString());
			}
		}

		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		paramMap.put("orderby","w.update_time desc,w.create_time desc");
		List<BWarehouse> selectWarehouseList = warehouseService.selectWarehouseList(paramMap);

		PageInfo<BWarehouse> pageInfo = new PageInfo<BWarehouse>(selectWarehouseList);// 传入list就可以了

		return R.ok().put("warehouseList", pageInfo);
	}
	@ResponseBody
	@GetMapping("/listAll")
	@RequiresPermissions("basics:bwarehouse:list")
	public R listAll() {

		//获取当前用户id
		SysUserEntity user = ShiroUtils.getUserEntity();
		SysUserEntity sysUserEntity1 = sysUserService.selectById(user.getUserId());
		//根据dept_id获得dept_no
		SysDeptEntity sysDeptEntity2 = sysDeptService.getDeptById(Long.parseLong(sysUserEntity1.getDeptId().toString()));
		//用戶下所有的仓库和id
		List<Map<String, Object>> WarnHouseMaps = warehouseService.selectAntennaByLikeDeptNo(sysDeptEntity2.getDeptNo());
		return R.ok().put("list",WarnHouseMaps);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("basics:bwarehouse:info")
	public R info(@PathVariable("id") Integer id){

		BWarehouse warehouse = warehouseService.selectByPrimaryKey(id);
		List<Long>longs=this.sysDeptService.getParenTListByDeptId(Long.valueOf(warehouse.getDepartmentId()));
		Collections.reverse(longs);
		warehouse.setDepartmentId(StringUtils.join(longs.toArray(), ","));
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("warehouse", warehouse);

		return R.ok().put("map", paramMap);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("basics:bwarehouse:save")
	public R save(@RequestBody JSONObject jsonObject){
		BWarehouse warehouse = jsonObject.getObject("warehouse", BWarehouse.class);

		SysUserEntity user = ShiroUtils.getUserEntity();
		
		warehouse.setCreateUserId(user.getUserId().intValue());
		int insert = warehouseService.insert(warehouse);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("basics:bwarehouse:update")
	public R update(@RequestBody JSONObject jsonObject){
		BWarehouse warehouse = jsonObject.getObject("warehouse", BWarehouse.class);
		
		if(warehouse.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = warehouseService.updateByPrimaryKeySelective(warehouse);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除.
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("basics:bwarehouse:delete")
	public R delete(@RequestBody Integer[] ids){
		List<Integer> idslist=new ArrayList<Integer>(Arrays.asList(ids));
		Map<String,Object> map=new HashMap<>();
		List<BWarehouse>list=warehouseService.selectIsUse(ids);
		StringBuffer stringBuffer=new StringBuffer();
		for (BWarehouse bwarehouse:list) {
			stringBuffer.append(bwarehouse.getAntennaName());
			idslist.remove(bwarehouse.getId());
			stringBuffer.append(",");
		}

		ids=idslist.toArray(new Integer[idslist.size()]);
		int delete=0;
		if(ids.length>0){
			delete= warehouseService.deleteBatchIds(ids);
		}
		if(list.size()>0){
			String msg=stringBuffer.toString().substring(0,stringBuffer.toString().length() - 1);
			map.put("msg",msg+"存在货区，无法删除");
		}else{
			map.put("msg","删除成功");
		}
		if(delete==ids.length){
			return R.ok().put("data",map);
		}

		return R.error();
	}

	@GetMapping("/getIsCheck/{id}")
	public R getIsCheck(@PathVariable(value = "id")Integer id){
		Boolean flag=this.warehouseService.getIsCheck(id);
		return R.ok().put("isCheck", flag);
	}

}
